<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\RechercheTable;
use Application\Model\Recherche;
use Application\Model\RechercheSejourTable;
use Application\Model\RechercheSejour;
use Zend\Session\Container; // We need this when using sessions
use Application\Model\ThematiquesTable;

class IndexController extends AbstractActionController
{
	protected $rechercheTable;
	protected $PaysTable;
	protected $RegionsTable;
	protected $AgenceTable;
	protected $ActivitesSejourTable;
	protected $FiltreTable;
	protected $RechercheSejourTable;
	protected $listeParametresURL = array("pays" => "pays", "region" => "region", "activite" => "activite", "agence" => "agence");
	protected $ThematiquesTable;
	
    public function indexAction()
    {

    }

    public function ChargerListesDonneesPageAccueilAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "GET"){
			// $pays = $this->getPaysTable()->fetchAllArray();			
			// $regions = $this->getRegionsTable()->fetchAllArray();
			// $types = $this->getTypesSejourTable()->fetchAllArray();
			// $activites = $this->getActivitesSejourTable()->fetchAllArray();
			$thematiques = $this->getThematiquesTable()->fetchAllUsed();

			echo json_encode(array( "thematiques" => $thematiques ));
		}
	
		return $this->response;
	}



    public function FiltresRechercheAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "GET"){

			$filtreRequete = $this->params()->fromQuery('q');

			$listePays = $this->getFiltreTable()->rechercherParmisLesFiltres( $filtreRequete );

			echo json_encode(  $listePays );
		}
	
		return $this->response;
	}

	public function LancerLaRechercheAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){

			$filtresRecherche = json_decode(file_get_contents('php://input'), true);

			$sejoursFiltres = $this->getRechercheSejourTable()->filtrerLesSejours( $filtresRecherche );

			echo json_encode(  $sejoursFiltres );
		}
	
		return $this->response;
	}

	/* Retourne tous les filtres de la page d'accueil via Ajax */
	public function FiltresRechercheAccueilAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "GET"){
			$result = $this->getRechercheTable()->filtresRechercheAccueil();

			echo json_encode( $result );
		}
	
		return $this->response;
	}

	public function UpdateFiltresRechercheAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){
        	$filtreValues = json_decode(file_get_contents('php://input'), true);

			$result = $this->getRechercheTable()->miseAJourDesFiltres ($filtreValues, $cache = true);

			echo json_encode( $result );
		}
		
		return $this->response;
	}

	/*
		Traitement des paramètres passés dans l'URL
	*/
	public function UpdateFiltresRechercheViaUrlAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){
        	$filtreValues = json_decode(file_get_contents('php://input'), true);

        	$filtresSelectedByUrl = array();

        	foreach ($filtreValues as $key => $value) {

        		if( $key == $this->listeParametresURL["pays"] ) {
        			$datas = $this->getPaysTable()->getPaysByLibelle( $value );

        			if( isset($datas) && $datas != null ) {
		    			$id = $datas->idPays;
						
		    			$listePaysId["paysId"] = array($id);
	    				$filtresSelectedByUrl = array( $listePaysId );
        			}
        		}
        		else
        		if($key == $this->listeParametresURL["region"]) {
        			$datas = $this->getRegionsTable()->getRegionByLibelle( $value );

        			if( isset($datas) && $datas != null ) {
		    			$id = $datas->idRegions;
						
		    			$listeId["regionId"] = array($id);
	    				$filtresSelectedByUrl = array( $listeId );
        			}

        		}
        		else
        		if($key == $this->listeParametresURL["activite"]) {
        			$datas = $this->getActivitesSejourTable()->getActiviteByLibelle( $value );

        			if( isset($datas) && $datas != null ) {
		    			$id = $datas->idAS;
						
		    			$listeId["activiteId"] = array($id);
	    				$filtresSelectedByUrl = array( $listeId );
        			}

        		}
        		else
        		if($key == $this->listeParametresURL["agence"]) {
        			$datas = $this->getAgenceTable()->getAgenceByLibelle( $value );

        			if( isset($datas) && $datas != null ) {
		    			$id = $datas->id;
						
		    			$listeId["agenceId"] = array($id);
	    				$filtresSelectedByUrl = array( $listeId );
        			}

        		}
        	}

        	if( count( $filtresSelectedByUrl ) > 0 ) {
				$result = $this->getRechercheTable()->miseAJourDesFiltres ($filtresSelectedByUrl, $cache = true);
        	} else {
        		$result = $this->InitialierFiltresListes ();
        	}

			echo json_encode( $result );
		}
		
		return $this->response;
	}

	public function UpdateFiltresRechercheNoCacheAction()
	{
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			$filtreValues = json_decode(file_get_contents('php://input'), true);

			$result = $this->getRechercheTable()->miseAJourDesFiltres ($filtreValues, $cache = false);

			echo json_encode( $result );
		}

		return $this->response;
	}

	/*************************************************************************************************************/

	public function getRechercheTable()
	{
	    if (!$this->rechercheTable) {
	        $sm = $this->getServiceLocator();
	        $this->rechercheTable = $sm->get('Application\Model\RechercheTable');
	    }
	    return $this->rechercheTable;
	}

	public function getPaysTable()
	{
		if( !$this->PaysTable ){
			$sm = $this->getServiceLocator();
			$this->PaysTable = $sm->get("Application\Model\PaysTable");
		}
		return $this->PaysTable;
	}
	
	public function getRegionsTable()
	{
		if( !$this->RegionsTable ){
			$sm = $this->getServiceLocator();
			$this->RegionsTable = $sm->get("Application\Model\RegionsTable");
		}
		return $this->RegionsTable;
	}

	public function getAgenceTable()
	{
		if( !$this->AgenceTable ){
			$sm = $this->getServiceLocator();
			$this->AgenceTable = $sm->get("Application\Model\AgenceTable");
		}
		return $this->AgenceTable;
	}

	public function getActivitesSejourTable()
	{
		if( !$this->ActivitesSejourTable ){
			$sm = $this->getServiceLocator();
			$this->ActivitesSejourTable = $sm->get("Application\Model\ActivitesSejourTable");
		}
		return $this->ActivitesSejourTable;
	}

	public function getFiltreTable()
	{
		if( !$this->FiltreTable ){
			$sm = $this->getServiceLocator();
			$this->FiltreTable = $sm->get("Application\Model\FiltreTable");
		}
		return $this->FiltreTable;
	}

	public function getRechercheSejourTable()
	{
		if( !$this->RechercheSejourTable ){
			$sm = $this->getServiceLocator();
			$this->RechercheSejourTable = $sm->get("Application\Model\RechercheSejourTable");
		}
		return $this->RechercheSejourTable;
	}

	public function getThematiquesTable()
	{
		if( !$this->ThematiquesTable ){
			$sm = $this->getServiceLocator();
			$this->ThematiquesTable = $sm->get("Application\Model\ThematiquesTable");
		}
		return $this->ThematiquesTable;
	}
}
